<div class="layui-fluid layui-anim febs-anim" id="febs-bookIndent" lay-title="订单页面"
     xmlns:shiro="http://www.w3.org/1999/xhtml">
  <div class="layui-row febs-container">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body febs-table-full">
          <form class="layui-form layui-table-form" lay-filter="bookIndent-table-form">
            <div class="layui-row">
              <div class="layui-col-md10">
                <div class="layui-form-item">
                  <div class="layui-inline" shiro:hasAnyPermissions="bookIndent:update">
                    <label class="layui-form-label layui-form-label-sm">买家姓名</label>
                    <div class="layui-input-inline">
                      <input type="text" name="username" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">图书名称</label>
                    <div class="layui-input-inline">
                      <input type="text" name="bookname" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">图书类型</label>
                    <div class="layui-input-inline">
                      <select name="type"
                              lay-verify="required"
                              xm-select-direction="down"
                              xm-select="insure-type"
                              xm-select-skin="default">
                      </select>
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                  <i class="layui-icon">&#xe848;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                  <i class="layui-icon">&#xe79b;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                     shiro:hasAnyPermissions="bookIndent:add,bookIndent:update">
                  <i class="layui-icon">&#xe875;</i>
                </div>
              </div>
            </div>
          </form>
          <table lay-filter="bookIndentTable" lay-data="{id: 'bookIndentTable'}"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="bookIndent-status">
  {{# if(d.status==0){}}
  <div><span style="color: #ff5652">未发货</span></div>
  {{# }else{ }}
  <div><span style="color: #85ef47"></span>{{d.status}}</div>
  {{# } }}
</script>
<script type="text/html" id="bookIndent-finishtime">
  {{# if(d.finishtime==0){}}
  <div><span style="color: #ff5652">未签收</span></div>
  {{# }else{ }}
  <div><span style="color: #00F7DE"></span>{{d.finishtime}}</div>
  {{# } }}
</script>
<script type="text/html" id="bookIndent-backstatus">
  {{#
  var backstatus = {
  0 : {title: '正常', color: 'green'},
  1 : {title: '报损申请中...', color: 'volcano'},
  2 : {title: '报损驳回', color: 'blue'},
  3 : {title: '退书申请中', color: 'magenta'},
  }[d.backstatus];
  }}
  <span class="layui-badge febs-tag-{{backstatus.color}}">{{ backstatus.title }}</span>
</script>
<script type="text/html" id="bookIndent-option-one" xmlns:shiro="http://www.w3.org/1999/xhtml">
  <span shiro:lacksPermission="bookIndent:flag,bookIndent:status,bookIndent:back">
        <span class="layui-badge-dot febs-bg-orange febs-orange"></span>无权限
    </span>
  <a lay-event="status" title="发货" shiro:hasPermission="bookIndent:status">
    <i class="layui-icon febs-edit-area febs-green">&#xe62f;</i></a>
  <a lay-event="flag" title="签收" shiro:hasPermission="bookIndent:flag"><i
          class="layui-icon febs-edit-area febs-orange">&#xe6af;</i></a>
  <a lay-event="back" title="报损退书" shiro:hasPermission="bookIndent:back"><i
          class="layui-icon febs-edit-area febs-red">&#x1007;</i></a>
</script>
<script type="text/html" id="bookIndent-option-two" xmlns:shiro="http://www.w3.org/1999/xhtml">
  <span shiro:lacksPermission="bookIndent:update,bookIndent:delete,bookIndent:apply">
        <span class="layui-badge-dot febs-bg-orange"></span>无权限
    </span>
  <a lay-event="edit" title="修改" shiro:hasPermission="bookIndent:update"><i
          class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
  <a lay-event="del" title="删除" shiro:hasPermission="bookIndent:delete">
    <i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
  layui.use(['dropdown', 'jquery', 'laydate', 'form', 'table', 'febs', 'formSelects'], function () {
    var $ = layui.jquery,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        dropdown = layui.dropdown,
        formSelects = layui.formSelects,
        $view = $('#febs-bookIndent'),
        $query = $view.find('#query'),
        $reset = $view.find('#reset'),
        $searchForm = $view.find('form'),
        sortObject = {field: 'finishtime', type: 'asc'},
        tableIns;

    form.render();

    initTable();

    formSelects.render();
    formSelects.config('insure-type', {
      searchUrl: ctx + 'bookMessage/dlist',
      response: {
        statusCode: 200
      },
      beforeSuccess: function (id, url, searchVal, result) {
        var data = result.data;
        var tranData = [];
        for (var i = 0; i < data.length; i++) {
          tranData.push({
            name: data[i].type,
            value: data[i].type
          })
        }
        result.data = tranData;
        return result;
      },
      error: function (id, url, searchVal, err) {
        console.error(err);
        febs.alert.error('获取书籍类型失败');
      }
    });

    dropdown.render({
      elem: $view.find('.action-more'),
      click: function (name, elem, event) {
        var checkStatus = table.checkStatus('bookIndentTable');
        if (name === 'print') {
          if (checkStatus.data.length===0) {
            febs.alert.warn('请先选择要退货的书籍');
          } else if (checkStatus.data.length>1){
            febs.alert.warn('请选择一条订单！');
          }else {
            febs.modal.open('退书信息登记', 'management/sendBookBack/add/' + checkStatus.data[0].id, {
              btn: ['提交', '退出'],
              area: $(window).width() <= 750 ? '95%' : '50%',
              yes: function (index, layero) {
                $('#sendBookBack-add').find('#submit').trigger('click');
              },
              btn2: function () {
                layer.closeAll();
              }
            });
          }
        }
        if (name === 'delete') {
          if (!checkStatus.data.length) {
            febs.alert.warn('请选择需要删除的记录');
          } else {
            febs.modal.confirm('删除信息', '确定删除该条？', function () {
              var bookIndentIds = [];
              layui.each(checkStatus.data, function (key, item) {
                bookIndentIds.push(item.id)
              });
              deletebookIndents(bookIndentIds.join(','));
            });
          }
        }
      },
      options: [{
        name: 'print',
        title: '退书登记',
      }, {
        name: 'delete',
        title: '删除书籍',
        perms: 'bookIndent:delete'
      }]
    });

    table.on('tool(bookIndentTable)', function (obj) {
      var data = obj.data,
          layEvent = obj.event;
      if (layEvent === 'del') {
        febs.modal.confirm('删除书籍信息', '确定删除该书籍信息？', function () {
          deletebookIndents(data.id);
        });
      }
      if (layEvent === 'flag') {
        febs.modal.confirm('确认收货', '确定该订单已经完成？', function () {
          flagbookIndents(data.id);
        });
      }
      if (layEvent === 'status') {
        febs.modal.confirm('发货成功', '确定该书籍已经发货？', function () {
          statusbookIndents(data.id);
        });
      }
      if (layEvent === 'edit') {
        febs.modal.open('修改书籍信息', 'business/bookIndent/update/' + data.id, {
          area: $(window).width() <= 750 ? '90%' : '50%',
          btn: ['提交', '取消'],
          yes: function (index, layero) {
            $('#bookIndent-update').find('#submit').trigger('click');
          },
          btn2: function () {
            layer.closeAll();
          }
        });
      }
      if (layEvent === 'back') {
        febs.modal.open('申请退书', 'business/bookIndent/back/' + data.id, {
          area: $(window).width() <= 750 ? '90%' : '50%',
          btn: ['提交', '取消'],
          yes: function (index, layero) {
            $('#bookIndent-back').find('#submit').trigger('click');
          },
          btn2: function () {
            layer.closeAll();
          }
        });
      }
    });


    table.on('sort(bookIndentTable)', function (obj) {
      sortObject = obj;
      tableIns.reload({
        initSort: obj,
        where: $.extend(getQueryParams(), {
          field: obj.field,
          order: obj.type
        })
      });
    });

    $query.on('click', function () {
      var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
      tableIns.reload({where: params, page: {curr: 1}});
    });

    $reset.on('click', function () {
      $searchForm[0].reset();
      tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
    });

    function initTable() {
      tableIns = febs.table.sell({
        elem: $view.find('table'),
        id: 'bookIndentTable',
        url: ctx + 'bookIndent/list',
        cols: [[
          {type: 'checkbox', fixed: 'true'},
          {field: 'username', title: '买家姓名', minWidth: 100, fixed: 'true'},
          {field: 'phone', title: '手机号码'},
          {field: 'address', title: '收货地址'},
          {field: 'bookname', title: '图书名称', minWidth: 100},
          {title: '出版社', field: 'pubid'},
          {title: '图书类型', field: 'type'},
          {field: 'bookmoney', title: '单价'},
          {field: 'number', title: '购买数量'},
          {field: 'money', title: '总费用'},
          {field: 'onetime', title: '下单时间'},
          {title: '发货时间', templet: '#bookIndent-status', sort: true},
          {title: '签收时间', templet: '#bookIndent-finishtime', sort: true},
          {title: '图书状态', templet: '#bookIndent-backstatus'},
          {title: '管理', toolbar: '#bookIndent-option-one', minWidth: 140, fixed: 'right'},
          {title: '操作', toolbar: '#bookIndent-option-two', minWidth: 140, fixed: 'right'}
        ]]
      }, currentUser.username);
    }

    function getQueryParams() {
      return {
        bookname: $searchForm.find('input[name="bookname"]').val().trim(),
        username: $searchForm.find('input[name="username"]').val(),
        type: $searchForm.find('input[name="type"]').val(),
      };
    }

    function deletebookIndents(bookIndentIds) {
      febs.post(ctx + 'bookIndent/delete/' + bookIndentIds, null, function () {
        febs.alert.success('删除成功');
        $query.click();
      });
    }

    function flagbookIndents(bookIndentIds) {
      febs.post(ctx + 'bookIndent/flag/' + bookIndentIds, null, function () {
        febs.alert.success('收货成功');
        $query.click();
      });
    }

    function statusbookIndents(bookIndentIds) {
      febs.post(ctx + 'bookIndent/status/' + bookIndentIds, null, function () {
        febs.alert.success('发货成功');
        $query.click();
      });
    }
  })
</script>